<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:php="http://php.net/xsl">

<xsl:output method="text"/>

<xsl:template match="databases">
<xsl:text>
/**
 * OORDBConnection class to </xsl:text><xsl:value-of select="attribute::user"/>@<xsl:value-of select="attribute::server"/><xsl:text>.
 */
class OORDBMySQLConnection extends OORDBConnection
{
	private $connection;
	private $server;
	private $user;
	
	public function __construct( $server, $user, $password )
	{
		$this->connection = mysql_connect( $server, $user, $password );
		$this->server = $server;
		$this->user = $user;
	}
	
	public function getServer()
	{
		return $this->server;
	}
	
	public function getUser()
	{
		return $this->user;
	}
	
	public function getDatabases()
	{
		$databases = new ArrayObject();</xsl:text>
		<xsl:for-each select="database"><xsl:text>
		$databases->append( new OORDB</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>Database( $this ) );</xsl:text>
		</xsl:for-each>
		<xsl:text>		
		return $databases->getIterator();
	}</xsl:text>
		<xsl:for-each select="database"><xsl:text>

	public function get</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>Database()
	{
		return new OORDB</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>Database( $this );</xsl:text>
	}</xsl:for-each>
<xsl:text>
	
	public function newQuery()
	{
		return new OORDBMySQLQuery( $this->connection );
	}
	
	public function newInsertQuery()
	{
		return new OORDBMySQLInsertQuery( $this->connection );
	}
	
	public function newSelectQuery()
	{
		return new OORDBMySQLSelectQuery( $this->connection );
	}
}
</xsl:text>
<xsl:apply-templates select="database"/>
</xsl:template>

<xsl:template match="database">
<xsl:text>
/**
 * OORDBDatabase class for the </xsl:text><xsl:value-of select="attribute::name"/><xsl:text> database.
 */
class OORDB</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>Database extends OORDBDatabase
{
	private $connection;
	
	public function __construct( OORDBMySQLConnection $connection )
	{
		$this->connection = $connection;
	}
	
	public function getName()
	{
		return "</xsl:text><xsl:value-of select="attribute::name"/><xsl:text>";
	}
	
	public function getConnection()
	{
		return $this->connection;
	}
	
	public function getTables()
	{
		$tables = new ArrayObject();</xsl:text>
		<xsl:for-each select="table"><xsl:text>
		$tables->append( new OORDB</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>Table( $this ) );</xsl:text>
		</xsl:for-each>
		<xsl:text>
		return $tables->getIterator();
	}</xsl:text>
		<xsl:for-each select="table"><xsl:text>

	public function get</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>Table()
	{
		return new OORDB</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>Table( $this );</xsl:text>
	}</xsl:for-each>
<xsl:text>
}
</xsl:text>
<xsl:apply-templates select="table"/>
</xsl:template>

<xsl:template match="table">
<xsl:text>
/**
 * OORDBTable class for the </xsl:text><xsl:value-of select="attribute::name"/><xsl:text> table.
 */
class OORDB</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>Table extends OORDBTable
{
	private $database;
	private $connection;
	
	private static $fields = array(</xsl:text><xsl:for-each select="fields/field"><xsl:if test="position()>1">,</xsl:if><xsl:text> "</xsl:text><xsl:value-of select="attribute::name"/>"</xsl:for-each> <xsl:text>);
	
	public function __construct( OORDB</xsl:text><xsl:value-of select="php:function('camel_case', string(../attribute::name) )"/><xsl:text>Database $database )
	{
		$this->database = $database;
		$this->connection = $database->getConnection();		
	}
	
	/**
	 * Get a single row from the table.
	 * @param mixed $id Primary key of row
	 * @return OORDBWrapper Row wrapper class
	 */
	public function get( $id )
	{
		$query = $this->connection->newSelectQuery();
		$table = $this->database->getName() . '.' . "</xsl:text><xsl:value-of select="attribute::name"/><xsl:text>";
		$query->setSQL( "SELECT * FROM `$table` WHERE id = '".$query->escape($id)."'" );
		$query->execute();
		$iter = $query;
		if ( $iter->valid() )
		{
			$row = $iter->current();
			return new OORDB</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>Wrapper( $this, $row );
		}
		else
		{
			throw new OORDBRowNotFoundException( $this, $id );
		}
	}
	
	/**
	 * Get a list of all rows from the database.
	 * @return Iterator Iterator across a list of OORDBWrapper classes.
	 */
	public function getAll()
	{
		$query = $this->connection->newSelectQuery();
		$table = $this->database->getName() . '.' . "</xsl:text><xsl:value-of select="attribute::name"/><xsl:text>";
		$query->setSQL( "SELECT * FROM $table" );
		$query->execute();
		$rows = new ArrayObject();
		foreach ( $query as $row )
		{
			$rows->append( new OORDB</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>Wrapper( $this, $row ) );
		}
		return $rows->getIterator();
	}
	
	/**
	 * Get the name of the table.
	 * @return string Name of table
	 */
	public function getName()
	{
		return "</xsl:text><xsl:value-of select="attribute::name"/><xsl:text>";
	}
	
	/**
	 * Get the names of table fields.
	 * @return array Array of field names.
	 */
	public function getFields()
	{
		return new ArrayIterator( self::$fields );
	}
	
	/**
	 * Get the database this table belongs to.
	 * @return OORDBDatabase Parent database
	 */
	public function getDatabase()
	{
		return $this->database;
	}
}
</xsl:text>


class <xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>
{
</xsl:text>
<xsl:for-each select="fields/field">
<xsl:text>	private $</xsl:text><xsl:value-of select="php:function('camel_case_lower_first', string(attribute::name) )"/><xsl:text>;

	</xsl:text>public function set<xsl:value-of select="php:function('camel_case', string(attribute::name) )"/> ( $value )<xsl:text>
	{
		$this-></xsl:text><xsl:value-of select="php:function('camel_case_lower_first', string(attribute::name) )"/><xsl:text> = $value;
	}

	</xsl:text>public function get<xsl:value-of select="php:function('camel_case', string(attribute::name) )"/> ()<xsl:text>
	{
		return $this-></xsl:text><xsl:value-of select="php:function('camel_case_lower_first', string(attribute::name) )"/><xsl:text>;
	}
	
</xsl:text>
</xsl:for-each>
<xsl:text>
}

/**
 * MySQL wrapper class for the </xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text> object.
 */
class OORDB</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>Wrapper extends OORDBWrapper
{
	private $object;
	private $table;
	
	public function __construct( OORDB</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>Table $table, $values )
	{
		$this->table = $table;
		$this->object = new </xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>();
		</xsl:text><xsl:for-each select="fields/field"><xsl:text>
		$this->object->set</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>( $values['</xsl:text><xsl:value-of select="attribute::name"/><xsl:text>'] );</xsl:text>
		</xsl:for-each>
<xsl:text>
	}
	
	/**
	 * Retrieve the object representing this row of data.
	 * @return object Object representing the row
	 */
	public function object()
	{
		return $this->object;
	}
	
	/**
	 * Save this row with values from its representing object.
	 */
	public function save()
	{
		
	}
	
	/**
	 * Remove this row from the table.
	 */
	public function remove()
	{
		
	}
	
	/**
	 * Retrieve an iterator over the fields' values.
	 * @return Iterator Iterator over row values
	 */
	public function values()
	{
		$values = new ArrayObject();</xsl:text>
		<xsl:for-each select="fields/field"><xsl:text>
		$values->append( $this->object->get</xsl:text><xsl:value-of select="php:function('camel_case', string(attribute::name) )"/><xsl:text>() );</xsl:text>
		</xsl:for-each>
<xsl:text>
		return $values->getIterator();
	}
	
	/**
	 * Get the table associated with this row.
	 * @return OORDBTable Table containing this row
	 */
	public function table()
	{
		return $this->table;
	}
}
</xsl:text>
</xsl:template>

</xsl:stylesheet>